package jbase.project.system.util;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import jbase.project.system.core.Sys;

public class HttpUtil {

	static public String getRequestMethodsPath(String headers) {
		if (headers == null) {
			return null;
		}
		String result = null;
		String firstLine = headers.split("\\r\\n")[0];
		result = firstLine.split("\\s")[1];
		return result;
	}

	static public String getRequestMethod(String headers) {
		if (headers == null) {
			return null;
		}
		String result = null;
		String firstLine = headers.split("\\r\\n")[0];
		result = firstLine.split("\\s")[0];
		return result;
	}

	static public String getRequestVer(String headers) {
		if (headers == null) {
			return null;
		}
		String result = null;
		String firstLine = headers.split("\\r\\n")[0];
		result = firstLine.split("\\s")[2];
		return result;
	}

	public static int getContentLength(String headers) {
		int contentLength = -1;
		String lines[] = headers.split("\\r\\n");
		for (int i = 0; i < lines.length; i++) {
			String line = lines[i];
			if (line.matches(".*[Cc][Oo][Nn][Tt][Ee][Nn][Tt][\\-][Ll][Ee][Nn][Gg][Tt][Hh].*")) {
				Pattern p = Pattern.compile("\\d+");
				Matcher m = p.matcher(line);
				if (m.find()) {
					contentLength = Integer.parseInt(m.group());
					break;
				}
			}
		}
		return contentLength;
	}

	public static String getContentType(String headers) {
		String contentType = null;
		String lines[] = headers.split("\\r\\n");
		for (int i = 0; i < lines.length; i++) {
			String line = lines[i];
			if (line.matches("\\s*" + RegUtil.toRegxIC("Content-Type") + ".*")) {
				Pattern p = Pattern.compile(":");
				Matcher m = p.matcher(line);
				if (m.find()) {
					String[] inf = line.split(":");
					contentType = inf[1].trim();
					if (inf.length > 2) {
						contentType += ":" + inf[2];
					}
					break;
				}
			}
		}
		return contentType;
	}

	public static String getRequestHost(String headers) {
		String host = null;
		String lines[] = headers.split("\\r\\n");
		for (int i = 0; i < lines.length; i++) {
			String line = lines[i];
			if (line.matches("\\s*[Hh][Oo][Ss][Tt][:].*")) {
				Pattern p = Pattern.compile(":");
				Matcher m = p.matcher(line);
				if (m.find()) {
					String[] inf = line.split(":");
					host = inf[1].trim();
					if (inf.length > 2) {
						host += ":" + inf[2];
					}
					break;
				}
			}
		}
		return host;
	}

	public static int getResponseStatus(String headers) {
		try {
			if (headers == null) {
				return -1;
			}
			String result = null;
			String firstLine = headers.split("\\r\\n")[0];
			result = firstLine.split("\\s")[1];
			return Integer.parseInt(result);
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public static String getRequestRelativeMethodLocation(String headers) {
		String methodsPath = getRequestMethodsPath(headers);
		if (methodsPath.matches("\\s*" + RegUtil.toRegxIC("http") + "s?" + "://[^/]+.*")) {
			Pattern p = Pattern.compile("\\s*" + RegUtil.toRegxIC("http") + "s?" + "://[^/]+");
			Matcher m = p.matcher(methodsPath);
			if (m.find()) {
				String preBase = m.group();
				methodsPath = methodsPath.substring(preBase.length());
			}
		}
		return methodsPath;
	}

	public static String getConnection(String headers) {
		String connection = null;
		if (headers != null) {
			String lines[] = headers.split("\\r\\n");
			for (int i = 0; i < lines.length; i++) {
				String line = lines[i];
				if (line.matches("\\s*" + RegUtil.toRegxIC("Connection") + "\\s*:" + ".*")) {
					Pattern p = Pattern.compile(":");
					Matcher m = p.matcher(line);
					if (m.find()) {
						connection = line.split(":")[1].trim();
						break;
					}
				}
			}
		}
		return connection;
	}

	public static String getProxyConnection(String headers) {
		String connection = null;
		if (headers != null) {
			String lines[] = headers.split("\\r\\n");
			for (int i = 0; i < lines.length; i++) {
				String line = lines[i];
				if (line.matches("\\s*" + RegUtil.toRegxIC("Proxy-Connection") + "\\s*:" + ".*")) {
					Pattern p = Pattern.compile(":");
					Matcher m = p.matcher(line);
					if (m.find()) {
						connection = line.split(":")[1].trim();
						break;
					}
				}
			}
		}
		return connection;
	}

	// Transfer-Encoding: chunked
	public static String getResponseTransferEncoding(String headers) {
		String transferEncoding = null;
		if (headers != null) {
			String lines[] = headers.split("\\r\\n");
			for (int i = 0; i < lines.length; i++) {
				String line = lines[i];
				if (line.matches("\\s*" + RegUtil.toRegxIC("Transfer-Encoding:") + ".*")) {
					Pattern p = Pattern.compile(":");
					Matcher m = p.matcher(line);
					if (m.find()) {
						transferEncoding = line.split(":")[1].trim();
						break;
					}
				}
			}
		}
		return transferEncoding;
	}

	public static String getResponseConnection(String headers) {
		String connection = null;
		if (headers != null) {
			String lines[] = headers.split("\\r\\n");
			for (int i = 0; i < lines.length; i++) {
				String line = lines[i];
				if (line.matches("\\s*" + RegUtil.toRegxIC("Connection") + "\\s*:" + ".*")) {
					Pattern p = Pattern.compile(":");
					Matcher m = p.matcher(line);
					if (m.find()) {
						connection = line.split(":")[1].trim();
						break;
					}
				}
			}
		}
		return connection;
	}

	public static String buildHeader(String headers) {
		try {
			String s = "";
			String[] lines = headers.split("\\r\\n");
			for (int i = 0; i < lines.length; i++) {
				String line = lines[i];
				if (line != null && !line.trim().equals("") && line.trim().length() > 0) {
					s += line + "\r\n";
				}
			}
			s += "\r\n";
			return s;
		} catch (RuntimeException e) {
			throw e;
		}
	}

	public static int calculateTrunkSize(String trunkHeader) {
		try {
			int size = -1;
			Pattern p = Pattern.compile("([a-zA-Z0-9])+");
			Matcher m = p.matcher(trunkHeader);
			if (m.find()) {
				size = Integer.valueOf(m.group(), 16);
			}
			return size;
		} catch (RuntimeException e) {
			throw e;
		}
	}

	// public static void main(String[] args) {
	// System.out.println(getRequestRelativeMethodLocation("GET
	// http://aaa.BB.cc:3344/fsagfsdg Http://1.1"));
	// }
}
